/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.NhatKyHT_Model;
import Models.KhachHang_Model;
import Models.LoaiKH_Model;
import Beans.NhanVien;
/**
 *
 * @author Venus
 */
public class KhachHang_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) {
                String item = request.getParameter("item");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSKH = khm.LayDSKH();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CreateCustomer")) {
                String lkh = request.getParameter("lkh");
                String tenkh = request.getParameter("tenkh");
                String diachi = request.getParameter("diachi");
                String sodt = request.getParameter("sodt");
                String birthday = request.getParameter("birthday");
                KhachHang_Model.TaoKhachHang(tenkh, lkh, diachi, sodt, birthday);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "customer");
                out.print("success");
            }
            else if (action.equals("ShowDSKH")) {
                String item = request.getParameter("item");
                KhachHang_Model khm = new KhachHang_Model();
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v1 = new Vector();
                v1 = khm.LayDSKH();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", v1);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortHoTenKH")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortHoTenKH(langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortDiemKH")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortDiemKH(langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortLoaiKH")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortLoaiKH(langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTTKH")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortTTKH(langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcustomer.jsp");
                rd.forward(request, response);
            }
             else if (action.equals("SearchCustomer")) {
                String hoten = request.getParameter("hoten");
                String sodiem = request.getParameter("sodiem");
                String loaikh = request.getParameter("loaikh");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();                
                vDSKH = khm.SearchCustomer(hoten, sodiem, loaikh, tinhtrang, langid);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("hoten", hoten);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("loaikh", loaikh);
                request.setAttribute("tinhtrang", tinhtrang);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchHoTenKH")) {
                String hoten = request.getParameter("hoten");
                String sodiem = request.getParameter("sodiem");
                String loaikh = request.getParameter("loaikh");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortSearchHoTenKH(hoten, sodiem, loaikh, tinhtrang, langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("hoten", hoten);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("loaikh", loaikh);
                request.setAttribute("tinhtrang", tinhtrang);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchDiemKH")) {
                String hoten = request.getParameter("hoten");
                String sodiem = request.getParameter("sodiem");
                String loaikh = request.getParameter("loaikh");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortSearchDiemKH(hoten, sodiem, loaikh, tinhtrang, langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("hoten", hoten);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("loaikh", loaikh);
                request.setAttribute("tinhtrang", tinhtrang);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchLoaiKH")) {
                String hoten = request.getParameter("hoten");
                String sodiem = request.getParameter("sodiem");
                String loaikh = request.getParameter("loaikh");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortSearchLoaiKH(hoten, sodiem, loaikh, tinhtrang, langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("hoten", hoten);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("loaikh", loaikh);
                request.setAttribute("tinhtrang", tinhtrang);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTTKH")) {
                String hoten = request.getParameter("hoten");
                String sodiem = request.getParameter("sodiem");
                String loaikh = request.getParameter("loaikh");
                String tinhtrang = request.getParameter("tinhtrang");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                KhachHang_Model khm = new KhachHang_Model();
                Vector vDSKH = new Vector();
                vDSKH = khm.SortSearchTTKH(hoten, sodiem, loaikh, tinhtrang, langid, order);
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", vDSKH);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "4");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("hoten", hoten);
                request.setAttribute("sodiem", sodiem);
                request.setAttribute("loaikh", loaikh);
                request.setAttribute("tinhtrang", tinhtrang);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateStatusKH")) {
                String tinhtrang = request.getParameter("tinhtrang");
                String makh = request.getParameter("makh");
                String item = request.getParameter("item");
                KhachHang_Model.UpdateStatusKH(tinhtrang, langid, makh);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statuscustomer");
                KhachHang_Model khm = new KhachHang_Model();
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v1 = new Vector();
                v1 = khm.LayDSKH();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", v1);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateLKH")) {
                String malkh = request.getParameter("malkh");
                String makh = request.getParameter("makh");
                String item = request.getParameter("item");
                KhachHang_Model.UpdateLKH(malkh, langid, makh);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "customertype");
                KhachHang_Model khm = new KhachHang_Model();
                LoaiKH_Model lkhm = new LoaiKH_Model();
                Vector v1 = new Vector();
                v1 = khm.LayDSKH();
                Vector vDSLKH = new Vector();
                vDSLKH = lkhm.LayDSLKH(langid);
                request.setAttribute("DSKH", v1);
                request.setAttribute("DSLKH", vDSLKH);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Detail")) {
                String makh = request.getParameter("makh");
                KhachHang_Model khm = new KhachHang_Model();
                Vector v = new Vector();
                v = khm.Detail(makh);
                request.setAttribute("detail", v);                
                request.setAttribute("makh", makh);
                RequestDispatcher rd = request.getRequestDispatcher("admin/detailcustomer.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateDetailkh")) {
                String tenkh = request.getParameter("tenkh");
                String diachi = request.getParameter("diachi");
                String sodt = request.getParameter("sodt");
                String birthday = request.getParameter("birthday");
                String loaikh = request.getParameter("loaikh");
                String makh = request.getParameter("makh");
                KhachHang_Model.UpdateDetailkh(makh, tenkh, diachi, sodt, birthday, loaikh);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "customerinfo");
                out.print("success");
            }
            else if (action.equals("DoiDiem")) {
                int x = KhachHang_Model.LaySoTien();
                int y = KhachHang_Model.LaySoDiem();
                String sotien = Integer.toString(x);
                String sodiem = Integer.toString(y);
                request.setAttribute("sotien", sotien );
                request.setAttribute("sodiem", sodiem);
                RequestDispatcher rd = request.getRequestDispatcher("admin/doidiem.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdateDoiDiem")) {
                String sodiem = request.getParameter("sodiem");
                String sotien = request.getParameter("sotien");
                KhachHang_Model.UpdateDoiDiem(sodiem, sotien);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "changemark");
                out.print("success");
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
